/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MovieTrees.Tool;

import MovieTrees.Record.*;
import java.io.File;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 影片列表读取
 * @author wcss
 */
public class MovieTreeReader 
{
   public static final String movieRangeSplit = "----<<<movierange>>>----";
   public static final String movieInfoSplit = "-<<<movieinfo>>>-";
   public static final String movieCtrlSplit = "-<<<moviectrl>>>-";
   public static final String movieTreeSplit = "-<<<movietree>>>-";
   public static final String movieListSplit = "-<<<movielist>>>-";
   public static final String movieFlagSplit = "-<<<movieflag>>>-";
   
   /*
    *  影片列表范例：
    *  播放类型：downloadAndPlay（边下边看）  finishAndPlay（下载完成观看）
    *  下载类型：http(可以下载http和ftp的资源)   qvod(可以下载快播的资源)
    * ----<<<movierange>>>----
    * 影片名称-<<<movieflag>>>-影片ID
    * 下载类型-<<<moviectrl>>>-播放类型
    * 国内剧场-<<<movietree>>>-言情剧
    * 影片图片（一张）
    * -<<<movieinfo>>>-
    * 影片简介影片简介影片简介影片简介影片简介影片简介影片简介影片简介影片简介影片简介影片简介
    * dsfadafdfasf影片简介影片简介影片简介影片简介影片简介影片简介影片简介影片简介影片
    * -<<<movielist>>>-
    * 名称-<<<movielist>>>-下载地址
    * ----<<<movierange>>>----
    * 
    */
   
   
   /**
    * 
    * @param path
    * @return 
    */
   public static ArrayList<MovieInfo> readMovieList(String path)
   {
       ArrayList<MovieInfo> result = new ArrayList<MovieInfo>();
       File f = new File(path);
       if (f.exists())
       {
            try {
                String[] lines = JAppToolKit.JDataHelper.readAllLines(path);
                ArrayList<String> ranges = new ArrayList<String>();
                Boolean addline = false;
                for(String s : lines)
                {
                    if (s.trim().equals(MovieTreeReader.movieRangeSplit))
                    {
                        addline = !addline;
                    }
                    
                    if (addline)
                    {
                        ranges.add(s);
                    }else
                    {
                        if (ranges.size() > 0)
                        {
                            try
                            {
                               result.add(readMovieInfo(ranges));                               
                            }catch (Exception ex)
                            {
                                throw new Exception("Movie List not load!");
                            }finally
                            {
                                ranges.clear();
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(MovieTreeReader.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
       return result;
       
   }
   
   private static MovieInfo readMovieInfo(ArrayList<String> all)
   {
       ArrayList<String> liness = new ArrayList<String>();
       liness.addAll(all);
       
       MovieInfo mi = new MovieInfo();
       liness.remove(0);
       
       String[] name_and_id = liness.get(0).split(MovieTreeReader.movieFlagSplit);
       mi.movieName = name_and_id[0];
       mi.movieID = name_and_id[1];
       liness.remove(0);
       
       String[] download_and_play = liness.get(0).split(MovieTreeReader.movieCtrlSplit);
       mi.movieDownloadType = download_and_play[0];
       mi.moviePlayMode = download_and_play[1];
       liness.remove(0);
       
       String[] trees = liness.get(0).split(MovieTreeReader.movieTreeSplit);
       mi.movieTreePath.clear();
       for(String ss : trees)
       {
          mi.movieTreePath.add(ss);
       }
       liness.remove(0);
       
       mi.movieImage = liness.get(0);
       liness.remove(0);
       liness.remove(0);
       
       int listflag = liness.indexOf(MovieTreeReader.movieListSplit);
       for(int k = 0; k < listflag;k++)
       {
           mi.movieInfo += liness.get(k) + "<br>";
       }       
       for(int f = 0;f <= listflag;f++)
       {
           liness.remove(0);
       }
       
       for(String sss:liness)
       {
           VideoItem vi = new VideoItem();
           String[] videos = sss.split(MovieTreeReader.movieListSplit);
           vi.videoName = videos[0];
           vi.videoUrl = videos[1];
           mi.subItems.add(vi);
       }       
       return mi;
   }
   
   public static void main(String[] args)
   {
       ArrayList<MovieInfo> all = MovieTreeReader.readMovieList("/home/wcss/templist.lst");
       
   }
   
}
